草庐IT

Java 中 this 和 super 的用法总结

全部标签

javascript - 使用 ES6 类将实例方法传递给 super

据我了解,在调用super()之前,this在构造函数中不可用。不过,在引用实例方法时,我们需要在方法前加上this前缀。那么如何将实例方法传递给super()呢?例如在Phaserframework,有一个Button类(class)。构造函数接受点击事件的回调:ConstructornewButton(game,x,y,key,callback,callbackContext,overFrame,outFrame,downFrame,upFrame)callback-ThefunctiontocallwhenthisButtonispressed.callbackContext-T

javascript - 为什么将字符串作为 "this"传递会导致这种奇怪现象?

我试图理解为什么javascript会做一些(对我来说)意想不到的事情。这里有一些代码,纯粹是为了举例。换句话说,我实际上并不想扩展String(我实际上绑定(bind)到函数和东西)。所以这是没有库的纯JavaScript。vars='blah';String.prototype.foo=function(){console.log('this===s:',this===s);console.log('this==s:',this==s);console.log('typeofthis:',typeofthis);console.log('typeofs:',typeofs);con

javascript - 主干错误 : Uncaught TypeError: Object function (){ parent. apply(this, arguments); } 没有方法 'on'

知道为什么我在调用collection.fetch时会收到此错误吗?在这段代码中抛出:这是触发错误的代码:$(document).ready->SearchResult=Backbone.Model.extendSearchResults=Backbone.Collection.extendurl:"/backbone/search"model:SearchResultparse:(response)->console.logresponsenewSearchResultid:response.idtitle:response.titlesearchResults=newSearchR

javascript - 为什么此代码有效 : "(1,eval)(' this')"

为什么下一个代码是有效的Javascript代码?varglobal=(1,eval)('this');alert(global); 最佳答案 那是因为commaoperator返回它的第二个操作数(并计算两者)。您问题中的代码相当于:1;varglobal=eval('this');alert(global); 关于javascript-为什么此代码有效:"(1,eval)('this')",我们在StackOverflow上找到一个类似的问题: https

javascript - react : How to access refs in this. props.children

我想调用一个子组件的函数。是否有可能在React中从this.props.children获取引用。varComponentSection=React.createClass({componentDidMount:function(){//Howtoaccessrefsinthis.props.children?this.refs.inner.refs.specificPanel.resize();},render:function(){return({this.props.children});}});varPanel=React.createClass({resize:functi

javascript - 为什么箭头函数的 'this' 在嵌套对象字面量中没有变化?

这个问题在这里已经有了答案:MethodsinES6objects:usingarrowfunctions(6个答案)关闭6年前。我发现在嵌套对象文字中使用箭头函数时,“this”关键字似乎总是指向global。根据其他问题,以下代码片段可以解释为箭头函数的“this”是在词法上下文中定义的。varc=100;vara={c:5,fn:()=>{returnthis.c;}};console.log(a.c);//100但是,我无法理解以下代码(嵌套对象字面量):varc=100;vara={c:5,b:{c:10,fn:()=>{returnthis.c;}}}console.log

javascript - backbone.js View 继承。 `this` 父级分辨率

我有一个使用View继承的案例,我的代码基本上是这样的:parentView=Backbone.View.extend({events:{"someevent":"business"},initialize:function(){_.bindAll(this);},business:function(e){...this.someFunc&&this.someFunc();...}});childView=parentView.extend({events:{...},constructor:function(){this.events=_.extend({},parentView.p

javascript - 从子类调用父类(super class)方法 - JavaScript

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:setattributewithjavascriptsupermethod为了好玩,我正在尝试用HTML5创建一个简单的游戏。我有一个Entity类,它应该是Player类的父类(superclass)。functionEntity(x,y){this.x=x;this.y=y;this.tick=function(){//Dogenericstuff}}functionPlayer(x,y){this.parent.constructor.call(this,x,y);this.tick=function(

javascript - Typescript 错误引用 _this

我尝试在TypeScript中为String.Prototype定义一些属性:Object.defineProperty(String.prototype,'test',{value:()=>{console.log("thisisatestovertext"+this);}})在javaScript原型(prototype)中,this指调用方法的对象(在本例中为字符串值)。但是文件的编译输出是:var_this=this;Object.defineProperty(String.prototype,'test',{value:function(){console.log("this

javascript - this.state 与 React 中的状态

我在一个新的代码库中工作。通常,我会在React组件中设置这样的状态:classAppextendsReact.Component{constructor(){super();this.state={foo:'bar'}}....在这个新的代码库中,我看到了很多这样的东西:classAppextendsReact.Component{state={foo:'bar'}....这样做有好处吗?他们似乎只在不需要更改状态时才这样做。我一直认为状态是React处理的东西。这是一件好事吗? 最佳答案 两种方法的最终结果是相同的。这两种方法都